package com.clickpaas.governmentcloud.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clickpaas.governmentcloud.common.ServerResponse;
import com.clickpaas.governmentcloud.entity.policyEntity.MatchingResults;
import com.clickpaas.governmentcloud.entity.policyEntity.PolicyEntity;
import com.clickpaas.governmentcloud.service.policyservice.PolicyService;
import com.clickpaas.governmentcloud.utils.portalutils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
public class BatchQueryMatchResultCallable implements Callable<List<MatchingResults>> {

    private PolicyEntity policyEntity;
    private PolicyService policyService;
    private Integer pageNum = 1;
    private Integer pageSize = 10;
    public BatchQueryMatchResultCallable(PolicyEntity policyEntity,PolicyService policyService){
        this.policyEntity = policyEntity;
        this.policyService = policyService;
    }

    @Override
    public List<MatchingResults> call() throws Exception {
        if (StringUtils.isBlank(policyEntity.getCorpName())) return null;
        ServerResponse<?> serverResponse = policyService.searchMatchingResult(pageNum, pageSize, policyEntity.getCorpName());
        if (200 == serverResponse.getcode()) {
            JSONObject jsonObject = JsonUtils.objToJson(serverResponse.getData());
            JSONArray jsonArray = jsonObject.getJSONArray("listMatchResult");
            String js = jsonArray.toJSONString();
            List<MatchingResults> listMatchResult = JSONObject.parseArray(js, MatchingResults.class);
            if (listMatchResult.size() > 0) {
                return listMatchResult;
            }
        }
        return null;
    }
}
